package hut.game.unit;

import hut.game.table.BanCo;
import hut.game.table.VanCo;
import android.widget.ImageView;

public class Tuong extends QuanCo {
	public Tuong() {
		super();
	}
	@Override
	public void KhoiTao(int phe, String name, String thutu, int stt,
			boolean khoa, int x, int y) {
		super.KhoiTao(phe, name, thutu, 1, khoa, x, y);
	}
	// Danh cho Computer
	private int diem = 0;

	public int getDiem() {
		if (this.TrangThai != 0) {
			if (this.Hang == 1 || this.Hang == 8)
				diem = 15;
			if (this.Hang == 2 || this.Hang == 7)
				diem = 10;
			if (this.Hang == 0 || this.Hang == 9) {
				if (this.Cot == 3 || this.Cot == 5)
					diem = 30;
				else
					diem = 35;
			} else
				diem = 0;
		}else
			diem = -6000;
		return diem;
	}

	@Override
	public int KiemTra(int i, int j) {
		boolean turn = false;
		int ho, co;
		QuanCo temp;
		temp = new QuanCo();

		if ((i >= 0 && i <= 2 && j >= 3 && j <= 5) || (i >= 7 && i <= 9 && j >= 3 && j <= 5))
            if ((i == Hang + 1 && j == Cot) || (i == Hang - 1 && j == Cot) || (i == Hang && j == Cot + 1) || (i == Hang && j == Cot - 1))
            {
                if (BanCo.ViTri[i][j].Trong == true) turn = true;
                if (BanCo.ViTri[i][j].Trong == false)
                    if (BanCo.ViTri[i][j].Phe != this.Phe) turn = true;
            }
		// Kiểm tra lộ mặt tướng
		if (VanCo.state) {
			if (Phe == 0)
            {
                int ct=0;
                if (j == VanCo.n2.qTuong.Cot)
                {
                    if (BanCo.ViTri[i][j].Trong == true)
                    {
                        for (int t = Hang + 1; t < VanCo.n2.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                    }
                    else
                    {
                    	if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe)
                    	{
                    		for(int k=0; k < VanCo.n1.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n1.dsQuanCo.get(k);
                    		}
                    	}else{
                    		for(int k=0; k < VanCo.n2.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n2.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n2.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n2.dsQuanCo.get(k);
                    		}
                    	}
                        ho = this.Hang;
                        co = this.Cot;
                        VanCo.OCoTrong(Hang, Cot);
                        BanCo.ViTri[i][j].Trong = false;
                        BanCo.ViTri[i][j].Phe = Phe;
                        BanCo.ViTri[i][j].Ten = Ten;
                        BanCo.ViTri[i][j].ThuTu = ThuTu;
                        this.Hang = i;
                        this.Cot = j;                        
                        temp.TrangThai = 0;
                        temp.imgUnit.setVisibility(ImageView.INVISIBLE);

                        //kiem tra
                        for (int t = Hang + 1; t < VanCo.n2.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;

                        this.Hang = ho;
                        this.Cot = co;
                        VanCo.OCoTrong(i, j);
                        BanCo.ViTri[ho][co].Trong = false;
                        BanCo.ViTri[ho][co].Phe = Phe;
                        BanCo.ViTri[ho][co].Ten = Ten;
                        BanCo.ViTri[ho][co].ThuTu = ThuTu;
                        if (temp.Phe != 2)
                        {
                        	if(temp.isFirst && temp.Ten != "tuong")
            					temp.TrangThai = 2;
            				else
            					temp.TrangThai = 1;
                            temp.imgUnit.setVisibility(ImageView.VISIBLE);
                            BanCo.ViTri[i][j].Trong = false;
                            BanCo.ViTri[i][j].Ten = temp.Ten;
                            BanCo.ViTri[i][j].Phe = temp.Phe;
                            BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                        }
                    }
                }
            }
            if (Phe == 1)
            {
                int ct = 0;
                if (j == VanCo.n1.qTuong.Cot)
                {
                    if (BanCo.ViTri[i][j].Trong == true)
                    {
                        for (int t = Hang - 1; t > VanCo.n1.qTuong.Hang; t--)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                    }
                    else
                    {
                    	if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe)
                    	{
                    		for(int k=0; k < VanCo.n1.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n1.dsQuanCo.get(k);
                    		}
                    	}else{
                    		for(int k=0; k < VanCo.n2.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n2.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n2.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n2.dsQuanCo.get(k);
                    		}
                    	}
                        ho = this.Hang;
                        co = this.Cot;
                        VanCo.OCoTrong(Hang, Cot);
                        BanCo.ViTri[i][j].Trong = false;
                        BanCo.ViTri[i][j].Phe = Phe;
                        BanCo.ViTri[i][j].Ten = Ten;
                        BanCo.ViTri[i][j].ThuTu = ThuTu;
                        this.Hang = i;
                        this.Cot = j;
                        temp.TrangThai = 0;
                        temp.imgUnit.setVisibility(ImageView.INVISIBLE);

                        //kiem tra
                        for (int t = Hang - 1; t > VanCo.n1.qTuong.Hang; t--)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;

                        this.Hang = ho;
                        this.Cot = co;
                        VanCo.OCoTrong(i, j);
                        BanCo.ViTri[ho][co].Trong = false;
                        BanCo.ViTri[ho][co].Phe = Phe;
                        BanCo.ViTri[ho][co].Ten = Ten;
                        BanCo.ViTri[ho][co].ThuTu = ThuTu;
                        if (temp.Phe != 2)
                        {
                        	if(temp.isFirst && temp.Ten != "tuong")
            					temp.TrangThai = 2;
            				else
            					temp.TrangThai = 1;
                            temp.imgUnit.setVisibility(ImageView.VISIBLE);
                            BanCo.ViTri[i][j].Trong = false;
                            BanCo.ViTri[i][j].Ten = temp.Ten;
                            BanCo.ViTri[i][j].Phe = temp.Phe;
                            BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                        } 
                    }
                }
			}
		} else { // Che do PvsC
			if (Phe == 0)
            {
                int ct=0;
                if (j == VanCo.n1.qTuong.Cot)
                {
                    if (BanCo.ViTri[i][j].Trong == true)
                    {
                        for (int t = Hang + 1; t < VanCo.n1.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                    }
                    else
                    {
                    	if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe)
                    	{
                    		for(int k=0; k < VanCo.n1.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n1.dsQuanCo.get(k);
                    		}
                    	}else{
                    		for(int k=0; k < VanCo.computer.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.computer.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.computer.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.computer.dsQuanCo.get(k);
                    		}
                    	}
                        ho = this.Hang;
                        co = this.Cot;
                        VanCo.OCoTrong(Hang, Cot);
                        BanCo.ViTri[i][j].Trong = false;
                        BanCo.ViTri[i][j].Phe = Phe;
                        BanCo.ViTri[i][j].Ten = Ten;
                        BanCo.ViTri[i][j].ThuTu = ThuTu;
                        this.Hang = i;
                        this.Cot = j;                        
                        temp.TrangThai = 0;
                        temp.imgUnit.setVisibility(ImageView.INVISIBLE);

                        //kiem tra
                        for (int t = Hang + 1; t < VanCo.n1.qTuong.Hang; t++)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;

                        this.Hang = ho;
                        this.Cot = co;
                        VanCo.OCoTrong(i, j);
                        BanCo.ViTri[ho][co].Trong = false;
                        BanCo.ViTri[ho][co].Phe = Phe;
                        BanCo.ViTri[ho][co].Ten = Ten;
                        BanCo.ViTri[ho][co].ThuTu = ThuTu;
                        if (temp.Phe != 2)
                        {
                        	if(temp.isFirst && temp.Ten != "tuong")
            					temp.TrangThai = 2;
            				else
            					temp.TrangThai = 1;
                            temp.imgUnit.setVisibility(ImageView.VISIBLE);
                            BanCo.ViTri[i][j].Trong = false;
                            BanCo.ViTri[i][j].Ten = temp.Ten;
                            BanCo.ViTri[i][j].Phe = temp.Phe;
                            BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                        }
                    }
                }
            }
            if (Phe == 1)
            {
                int ct = 0;
                if (j == VanCo.computer.qTuong.Cot)
                {
                    if (BanCo.ViTri[i][j].Trong == true)
                    {
                        for (int t = Hang - 1; t > VanCo.computer.qTuong.Hang; t--)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;
                    }
                    else
                    {
                    	if(BanCo.ViTri[i][j].Phe == VanCo.n1.Phe)
                    	{
                    		for(int k=0; k < VanCo.n1.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.n1.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.n1.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.n1.dsQuanCo.get(k);
                    		}
                    	}else{
                    		for(int k=0; k < VanCo.computer.dsQuanCo.size(); k++)
                    		{
                    			if(BanCo.ViTri[i][j].Ten == VanCo.computer.dsQuanCo.get(k).Ten && BanCo.ViTri[i][j].ThuTu == VanCo.computer.dsQuanCo.get(k).ThuTu)
                    				temp = VanCo.computer.dsQuanCo.get(k);
                    		}
                    	}
                        ho = this.Hang;
                        co = this.Cot;
                        VanCo.OCoTrong(Hang, Cot);
                        BanCo.ViTri[i][j].Trong = false;
                        BanCo.ViTri[i][j].Phe = Phe;
                        BanCo.ViTri[i][j].Ten = Ten;
                        BanCo.ViTri[i][j].ThuTu = ThuTu;
                        this.Hang = i;
                        this.Cot = j;
                        temp.TrangThai = 0;
                        temp.imgUnit.setVisibility(ImageView.INVISIBLE);

                        //kiem tra
                        for (int t = Hang - 1; t > VanCo.computer.qTuong.Hang; t--)
                        {
                            if (BanCo.ViTri[t][j].Trong == false) ct++;
                        }
                        if (ct == 0) turn = false;

                        this.Hang = ho;
                        this.Cot = co;
                        VanCo.OCoTrong(i, j);
                        BanCo.ViTri[ho][co].Trong = false;
                        BanCo.ViTri[ho][co].Phe = Phe;
                        BanCo.ViTri[ho][co].Ten = Ten;
                        BanCo.ViTri[ho][co].ThuTu = ThuTu;
                        if (temp.Phe != 2)
                        {
                        	if(temp.isFirst && temp.Ten != "tuong")
            					temp.TrangThai = 2;
            				else
            					temp.TrangThai = 1;
                            temp.imgUnit.setVisibility(ImageView.VISIBLE);
                            BanCo.ViTri[i][j].Trong = false;
                            BanCo.ViTri[i][j].Ten = temp.Ten;
                            BanCo.ViTri[i][j].Phe = temp.Phe;
                            BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
                        } 
                    }
                }
			}
		}
		if (turn) return 1;
		else return 0;
	}

	@Override
	public int TuongAnToan(int i, int j) {
		boolean turn = true;
		// Kiểm tra tướng an toàn
		if (turn == true) {
			int ho, co;
			QuanCo temp = null;
			temp = new QuanCo();

			ho = this.Hang;
			co = this.Cot;
			if (VanCo.state) {
				if (BanCo.ViTri[i][j].Trong == false) {
					if (BanCo.ViTri[i][j].Phe == VanCo.n1.Phe) {
						for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
							if (VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n1.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < VanCo.n2.dsQuanCo.size(); k++) {
							if (VanCo.n2.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten
									&& VanCo.n2.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n2.dsQuanCo.get(k);
						}
					}
				}
			} else {
				if (BanCo.ViTri[i][j].Trong == false) {
					if (BanCo.ViTri[i][j].Phe == VanCo.computer.Phe) {
						for (int k = 0; k < VanCo.computer.dsQuanCo.size(); k++) {
							if (VanCo.computer.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten	&& VanCo.computer.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.computer.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
							if (VanCo.n1.dsQuanCo.get(k).Ten == BanCo.ViTri[i][j].Ten && VanCo.n1.dsQuanCo.get(k).ThuTu == BanCo.ViTri[i][j].ThuTu)
								temp = VanCo.n1.dsQuanCo.get(k);
						}
					}
				}
			}
			// Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
			VanCo.OCoTrong(Hang, Cot);
			BanCo.ViTri[i][j].Trong = false;
			BanCo.ViTri[i][j].Phe = Phe;
			BanCo.ViTri[i][j].Ten = Ten;
			BanCo.ViTri[i][j].ThuTu = ThuTu;
			this.Hang = i;
			this.Cot = j;
			if (temp.Phe != 2) {
				temp.TrangThai = 0;
				temp.imgUnit.setVisibility(ImageView.INVISIBLE);
			}
			// Kiểm tra
			if (VanCo.state) {
				if (this.Phe == VanCo.n1.Phe) {
					if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
				} else {
					if (VanCo.ChieuTuong(VanCo.n2.qTuong) == true) turn = false;
				}
			} else {
				if (this.Phe == VanCo.n1.Phe) {
					if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true) turn = false;
				} else {
					if (VanCo.ChieuTuong(VanCo.computer.qTuong) == true) turn = false;
				}
			}
			// Trả lại những gì đã giả sử ở trên ^^!
			this.Hang = ho;
			this.Cot = co;
			VanCo.OCoTrong(i, j);
			BanCo.ViTri[ho][co].Trong = false;
			BanCo.ViTri[ho][co].Phe = Phe;
			BanCo.ViTri[ho][co].Ten = Ten;
			BanCo.ViTri[ho][co].ThuTu = ThuTu;
			if (temp.Phe != 2) {
				if(temp.isFirst && temp.Ten != "tuong")
					temp.TrangThai = 2;
				else
					temp.TrangThai = 1;
				temp.imgUnit.setVisibility(ImageView.VISIBLE);
				BanCo.ViTri[i][j].Trong = false;
				BanCo.ViTri[i][j].Ten = temp.Ten;
				BanCo.ViTri[i][j].Phe = temp.Phe;
				BanCo.ViTri[i][j].ThuTu = temp.ThuTu;
				BanCo.ViTri[i][j].TrangThai = temp.TrangThai;
				
			}
		}
		// Trả về kết quả
		if (turn) return 1;
		else return 0;
	}
}
